package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.MallIndexVo;
import com.yt.app.api.vo.ProductVo;
import com.yt.app.core.interfaces.AppBannerService;
import com.yt.app.core.model.AppBanner;
import com.yt.app.core.utils.Constant;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.interfaces.ProductService;
import com.yt.order.api.interfaces.ProductTypeService;
import com.yt.order.api.model.Institution;
import com.yt.order.api.model.Product;
import com.yt.order.api.model.ProductType;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "03.商城")
@RequestMapping("/mall")
public class MallController extends BaseAction{
    @Autowired
    private AppBannerService appBannerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private InstitutionService institutionService;

    @ApiOperation(value="商城首页接口", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "Banner属性", response = MallIndexVo.class)})
    @RequestMapping("/index")
    public @ResponseBody  Object bannerList(
            @ApiParam("上一页最后一个ID") @RequestParam(name = "lastId", required = false, defaultValue = "0") Long lastId
    ){
        MsgBean msgBean = new MsgBean();
        MallIndexVo vo = new MallIndexVo();
        List<AppBanner> bannerList = appBannerService.getValidBannerList(Constant.APP_BANNER_TYPE.Mall.value());
        vo.setBanners(bannerList);
        /*****************/
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter("Q_isPublic_S_EQ", "1");
        filter.addFilter("Q_onsale_S_EQ", "1");
        if(lastId > 0){
            filter.addFilter("Q_id_L_GT", String.valueOf(lastId));
        }
        List<ProductVo> voList = new ArrayList<ProductVo>();
        List<Product> productList = productService.find(filter);
        lastId = -1L;
        if(productList != null && !productList.isEmpty()){
            for(Product product : productList){
                voList.add(toProductVo(product));
            }
            lastId = voList.get(voList.size() - 1).getId();
        }
        vo.setProducts(voList);
        vo.setLastId(lastId);
        /*****************/
        msgBean.put("data", vo);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="商品详情页", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "商品属性", response = ProductVo.class)})
    @RequestMapping("/getProduct")
    public @ResponseBody  Object getProduct(
            @ApiParam("商品ID") @RequestParam(name = "productId", required = true) Long productId
    ){
        MsgBean msgBean = new MsgBean();
        Product product = productService.get(productId);
        if(product == null){
            return msgBean.failure("善品不存在!").returnMsg();
        }

        ProductVo vo = new ProductVo(product);

        ProductType productType = productTypeService.get(product.getProductTypeId());
        if(productType != null){
            vo.setProductTypeName(productType.getName());
        }
        Institution institution = institutionService.get(product.getIdInstitution());
        if(institution != null){
            vo.setInstitutionName(institution.getName());
        }
        msgBean.put("product", vo);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="商品列表", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "商品属性", response = ProductVo.class)})
    @RequestMapping("/listProduct")
    public @ResponseBody  Object getProduct(
            @ApiParam("分类ID") @RequestParam(name = "typeId", required = false, defaultValue = "0") Long typeId,
            @ApiParam("机构ID") @RequestParam(name = "institutionId", required = false, defaultValue = "0") Long institutionId,
            @ApiParam("上一页最后一个ID") @RequestParam(name = "lastId", required = false, defaultValue = "0") Long lastId
    ){
        MsgBean msgBean = new MsgBean();
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter("Q_isPublic_S_EQ", "1");
        filter.addFilter("Q_onsale_S_EQ", "1");
        if(lastId > 0){
            filter.addFilter("Q_id_L_GT", String.valueOf(lastId));
        }
        if(typeId > 0){
            filter.addFilter("Q_productTypeId_L_EQ", String.valueOf(typeId));
        }
        if(institutionId > 0){
        filter.addFilter("Q_idInstitution_L_EQ", String.valueOf(institutionId));
        }
        List<ProductVo> voList = new ArrayList<ProductVo>();
        List<Product> productList = productService.find(filter);
        lastId = -1L;
        if(productList != null && !productList.isEmpty()){
            for(Product product : productList){
                voList.add(toProductVo(product));
            }
            lastId = voList.get(voList.size() - 1).getId();
        }
        msgBean.put("lastId", lastId);
        msgBean.put("products", voList);
        return msgBean.returnMsg();
    }

    private ProductVo toProductVo(Product product){

        ProductVo vo = new ProductVo(product);

        ProductType productType = productTypeService.get(product.getProductTypeId());
        if(productType != null){
            vo.setProductTypeName(productType.getName());
        }
        Institution institution = institutionService.get(product.getIdInstitution());
        if(institution != null){
            vo.setInstitutionName(institution.getName());
        }
        return vo;
    }
}
